﻿using System;
using System.Collections;
using System.Web.Caching;
using System.Web.Hosting;
using Dexter.Storage;

namespace Dexter.Web.Mvc.ViewEngines {
	public class DexterVirtualPathProvider : VirtualPathProvider {
		readonly IStorageProvider storageProvider;

		public DexterVirtualPathProvider ( IStorageProvider storageProvider ) {
			this.storageProvider = storageProvider;
		}

		public override bool FileExists ( string virtualPath ) {
			var fullPath = storageProvider.GetPublicUrl ( virtualPath );

			if (storageProvider.FileExist ( fullPath ))
				return true;

			return Previous.FileExists ( virtualPath ); 
		}

		public override VirtualFile GetFile ( string virtualPath ) {
			var fullPath = storageProvider.GetPublicUrl ( virtualPath );

			if (storageProvider.FileExist ( fullPath ))
				return new DexterVirtualFile ( virtualPath, storageProvider );

			return Previous.GetFile ( virtualPath ); 
		}

		public override CacheDependency GetCacheDependency ( string virtualPath , IEnumerable virtualPathDependencies , DateTime utcStart ) {
			return null;
		}
	}
}